package com.nmsl.masterdocker.service.impl;
import com.nmsl.common.core.domain.AjaxResult;
import com.nmsl.masterdocker.domain.Inpatient;
import com.nmsl.inpatientjin.domain.JinPatient;
import com.nmsl.masterdocker.mapper.HospitalizationRegistrationZYMapper;
import com.nmsl.inpatientjin.mapper.JinPatientMapper;
import com.nmsl.sn.domain.SnVisitRecord;
import com.nmsl.sn.mapper.SnVisitRecordMapper;
import com.nmsl.system.domain.JinDepartments;
import com.nmsl.system.mapper.JinDepartmentsMapper;
import com.nmsl.system.mapper.JinEmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class InpatientServiceImpl {
    @Autowired
    private HospitalizationRegistrationZYMapper hospitalizationRegistrationZYMapper;
    @Autowired
    private JinPatientMapper mPatientMapper;

    @Autowired
    private JinDepartmentsMapper jinDepartmentsMapper;
    @Autowired
    private JinEmployeeMapper jinEmployeeMapper;
    @Autowired
    private SnVisitRecordMapper snVisitRecordMapper;

    public List<Inpatient> getAllInpatient(Long doctorId) {
        // 根据医生id查询住院登记信息
        return hospitalizationRegistrationZYMapper.selectpatientByDoctorId(doctorId).stream().map(hospitalizationRegistrationZY -> {
            // 创建住院患者对象
            Inpatient inpatient = new Inpatient();
            inpatient.setDepartmentId(hospitalizationRegistrationZY.getDepartmentId());
            // 设置患者id
            inpatient.setPatientId(hospitalizationRegistrationZY.getPatientId());
            // 根据患者id查询患者信息
            JinPatient Patient = mPatientMapper.selectJinPatientByPatientId(hospitalizationRegistrationZY.getPatientId());
            // 设置患者姓名
            inpatient.setPatientName(Patient.getRealName());
            // 设置患者年龄
            inpatient.setPatientAge(Patient.getAge());
            // 设置患者性别
            inpatient.setPatientSex(Patient.getGender());
            // 设置床号
            inpatient.setBedNum(hospitalizationRegistrationZY.getBedNumber());
            // 根据科室id查询科室名称
            JinDepartments departments = jinDepartmentsMapper.selectJinDepartmentsByDepartmentId(hospitalizationRegistrationZY.getDepartmentId());
            if (departments != null) {
                inpatient.setDepartment(departments.getDepartmentName());
            } else {
                inpatient.setDepartment("无科室");
            }

            // 根据医生id查询医生姓名
            inpatient.setDoctor(jinEmployeeMapper.selectJinEmployeeById(hospitalizationRegistrationZY.getDoctorId()).getName());
            // 根据就诊记录id查询诊断信息
            SnVisitRecord snVisitRecord=snVisitRecordMapper.selectSnVisitRecordByVisitRecordId(hospitalizationRegistrationZY.getVisitRecordId());
            if(snVisitRecord!=null){
                inpatient.setDiagnose(snVisitRecord.getPreliminaryDiagnosis());
            }else {
                inpatient.setDiagnose("无诊断信息");
            }

            // 设置入院日期
            inpatient.setDate(hospitalizationRegistrationZY.getAdmissionTime());
//    private Long patientId;//患者id
//    private String patientName;//患者姓名
//    private String patientSex;
//    private String patientAge;
//    private String bed;//床号
//    private String department;//科室
//    private String doctor;//主治医生
//    private String diagnose;//诊断
//    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//    private Date date;//入院日期
//    private String status;//状态
            return inpatient;
        }).collect(Collectors.toList());

    }
    public AjaxResult addInpatient(Inpatient inpatient){
        try{
//            HospitalizationRegistrationZY hospitalizationRegistrationZY=new HospitalizationRegistrationZY();
//            hospitalizationRegistrationZY.setPatientId(inpatient.getPatientId());
//            hospitalizationRegistrationZY.setAdmissionTime(inpatient.getDate());
//            hospitalizationRegistrationZY.setBedNumber(inpatient.getBedNum());
//            hospitalizationRegistrationZY.setDepartmentId(inpatient.getDepartmentId());
//            hospitalizationRegistrationZY.setDoctorId(inpatient.getDoctorId());
//            hospitalizationRegistrationZY.setVisitRecordId(inpatient.getVisitRecordId());
//            hospitalizationRegistrationZYMapper.insert(hospitalizationRegistrationZY);
            return AjaxResult.success("添加成功");

        }catch (Exception e){
            return AjaxResult.error("添加失败");
        }
    }
}
